/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rhdtp.demopf.model.dao;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Transaction;
import org.hibernate.Session;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public abstract class TemplateDAO<T> {

    private static final Logger logger = Logger.getLogger(TemplateDAO.class);
    private SessionFactoryImpl sessionFactory;

    public T save(T t) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        sessionFactory = (SessionFactoryImpl) context.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        try {
            session.save(t);
            tx.commit();
            return t;
        } catch (Exception ex) {
            tx.rollback();
            logger.error(ex.getCause(), ex);
            return null;
        } finally {
            session.clear();
            session.close();
        }
    }

    public T saveOrUpdate(T t) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        sessionFactory = (SessionFactoryImpl) context.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        try {
            session.saveOrUpdate(t);
            tx.commit();
            return t;
        } catch (Exception ex) {
            tx.rollback();
            logger.error(ex.getCause(), ex);
            return null;
        } finally {
            session.clear();
            session.close();
        }
    }

    public boolean update(T t) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        sessionFactory = (SessionFactoryImpl) context.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        try {
            session.update(t);
            tx.commit();
            return true;
        } catch (Exception ex) {
            tx.rollback();
            logger.error(ex.getCause(), ex);
            return false;
        } finally {
            session.clear();
            session.close();
        }
    }

    public boolean delete(T t) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        sessionFactory = (SessionFactoryImpl) context.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        try {
            session.delete(t);
            tx.commit();
            return true;
        } catch (Exception ex) {
            tx.rollback();
            logger.error(ex.getCause(), ex);
            return false;
        } finally {
            session.clear();
            session.close();
        }
    }

    public abstract T load(T t);

    public abstract List<T> list();

    public abstract List<T> search(T t);
}
